package com.lyy.photovoltaic.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lyy.photovoltaic.beans.LeaveMessage;
import com.lyy.photovoltaic.common.utils.MyRandom;
import com.lyy.photovoltaic.common.vo.ResultCode;
import com.lyy.photovoltaic.common.vo.ResultVO;
import com.lyy.photovoltaic.mapper.LeaveMessageMapper;
import com.lyy.photovoltaic.service.LeaveMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class LeaveMessageServiceImpl implements LeaveMessageService {
    @Autowired
    LeaveMessageMapper leaveMessageMapper;

    @Override
    public ResultVO addLeaveMessage(LeaveMessage leaveMessage) {
        System.out.println(leaveMessage);
        LeaveMessage leaveMessage1 = new LeaveMessage();
        leaveMessage1.setLeaveMessageId(System.currentTimeMillis()+ MyRandom.getStringRandom());
        leaveMessage1.setUserId(leaveMessage.getUserId());
        leaveMessage1.setUserName(leaveMessage.getUserName());
        leaveMessage1.setLmText(leaveMessage.getLmText());
        leaveMessage1.setLmTime(new Date());
        leaveMessage1.setStaffId("0");
        leaveMessage1.setLmReply("0");
        int i = leaveMessageMapper.insert(leaveMessage1);
        if (i > 0) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "添加留言信息成功",leaveMessage1);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED,"添加失败",null);
    }

    @Override
    public ResultVO updateLeaveMessage(String LMId,JSONObject jsonObject) {
        System.out.println(jsonObject);
        LeaveMessage leaveMessage = leaveMessageMapper.selectByPrimaryKey(LMId);
        leaveMessage.setStaffId(jsonObject.getString("staffId"));
        leaveMessage.setLmReply(jsonObject.getString("lmReply"));
        int i = leaveMessageMapper.updateByPrimaryKeySelective(leaveMessage);
        if (i > 0) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "留言留言回复成功",leaveMessage);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED,"留言留言回复失败",null);
    }

    @Override
    public ResultVO findNewLeaveMessage() {
        Example example = new Example(LeaveMessage.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("staffId","0");
        criteria.andEqualTo("lmReply","0");
        List<LeaveMessage> leaveMessages = leaveMessageMapper.selectByExample(example);
        System.out.println(leaveMessages);
        if (leaveMessages.size() > 0) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "查询成功",leaveMessages);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED,"查询失败",null);
    }

    @Override
    public ResultVO findRepliedLeaveMessage(String uId) {
        Example example = new Example(LeaveMessage.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",uId);
        criteria.andNotEqualTo("lmReply","0");
        List<LeaveMessage> leaveMessages = leaveMessageMapper.selectByExample(example);
        if (leaveMessages.size() > 0) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "查询成功",leaveMessages);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED,"查询失败",null);
    }
}
